package com.nuanshui.heatedloan.service.cms.security;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.dao.cms.security.RoleDao;
import com.nuanshui.heatedloan.entity.cms.security.Role;
import com.nuanshui.heatedloan.extension.shiro.ShiroDbRealm;
import com.nuanshui.heatedloan.service.cms.BaseService;
import com.nuanshui.heatedloan.util.dwz.DwzPage;
import com.nuanshui.heatedloan.util.springdata.PageUtils;



/**
 * 角色的服务类
 */
@Service
@Transactional(readOnly = true,value=TransactionManagerType.MANAGER_SERVICE_TRANSACTION_MARGER)
public class RoleService extends BaseService<Role, Long> {

	@Autowired
	private RoleDao roleDao;

	@Autowired(required = false)
	private ShiroDbRealm shiroRealm;

	@Override
	public List<Role> findAll(final DwzPage page) {
		final Page<Role> page2 = roleDao
				.findAll(PageUtils.createPageable(page));
		PageUtils.injectPageProperties(page, page2);
		return page2.getContent();
	}

	/**
	 * 修改
	 */
	@Transactional(value=TransactionManagerType.MANAGER_SERVICE_TRANSACTION_MARGER)
	public void updateRole(final Role role) {
		roleDao.save(role);
	}

	/**
	 * 删除
	 */
	@Override
	@Transactional(value=TransactionManagerType.MANAGER_SERVICE_TRANSACTION_MARGER)
	public void delete(final Long id) {
		roleDao.delete(id);
	}

	/**
	 * 根据名称查询
	 */
	public List<Role> find(final DwzPage page, final String name) {
		final org.springframework.data.domain.Page<Role> roles = roleDao
				.findByNameContaining(name, PageUtils.createPageable(page));
		PageUtils.injectPageProperties(page, roles);
		return roles.getContent();
	}

}
